const { open, login, preview, upload } = require("../util/xcx_http.js");
const { ipcRenderer } = require("electron");
const WatchFile = require("../util/watch_file");
const { xcx } = require("../../config");
const fse=require("fs-extra")
window.onload = async () => {
	fse.watchFile( xcx.loginTmp, (curr, prev) => {
		console.log(curr);
		console.log(prev);
	  });
	// const watchFile = new WatchFile({
	// 	file: xcx.loginTmp,
	// 	change: (event, name, body) => {
	// 		console.log(body);
	// 		watchFile.close();
	// 	},
	// 	close: () => {
	// 		console.log("close");
	// 	},
	// 	err: () => {
	// 		watchFile.close();
	// 	},
	// });
	let openIde = document.querySelector("#open-ide");
	let projectpath = "F:\\wx-workspace\\beautiful-table-mini";
	let openFolder = document.querySelector("#open-folder");
	let xcxPreview = document.querySelector("#xcx-preview");
	let xcxUpload = document.querySelector("#xcx-upload");
	let xcxLogin = document.querySelector("#xcx-login");

	openFolder.onclick = e => {
		try {
			ipcRenderer.send("open-directory-dialog");
		} catch (error) {
			console.log(error);
		}
	};
	ipcRenderer.on("xcx-select-folder", (event, file) => {
		try {
			const project_root = file[0];
			projectRoot = project_root;
			const { stdout } = cmdOpenTool(project_root);
			showExeMessage(stdout);
		} catch (error) {
			console.log(error);
		}
	});
	openIde.onclick = () => {
		open()
			.then(res => {
				console.log(res);
			})
			.catch(err => {
				console.log(err);
			});
	};
	xcxLogin.onclick = () => {
		login().then(([res, body]) => {
			console.log(body);
			showQrcode(body);
		});
	};
	xcxPreview.onclick = () => {
		preview({
			projectpath,
		}).then(([res, body]) => {
			console.log(res);
			showQrcode(`data:image/jpeg;base64,` + body);
		});
	};
	xcxUpload.onclick = () => {
		upload({
			projectpath,
			version: "1.0.1",
			desc: "test",
		}).then(([res, body]) => {
			console.log(res);
		});
	};
	/**
	 * 展示二维码
	 * @param {*} src
	 */
	function showQrcode(src = ``) {
		document.querySelector(".qrcode img").setAttribute("src", src);
	}
	/**
	 * 展示命令行信息
	 * @param {*} msg
	 */
	function showExeMessage(msg = "") {
		document.querySelector(".show-exex-message").innerHTML = msg;
	}
};
